package com.me.spider;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
public class BaiduDiskSpider implements PageProcessor, ApplicationContextAware {

    @Autowired
    private static ApplicationContext applicationContext;

    // 爬url
    List<String> linkList;
    // 爬内容
    List<String> contentList;
    // 爬日期
    List<String> dateList;

    private static int start = 372;//612
    private static int pageCount = 675;
    // 暂存符合匹配查询的详情页面
    private static ArrayList<String> arr = new ArrayList();
    /**
     * netty+傅健   65 675
     */
    private static String gp = "咕泡";// 612 2103
    private static String xx = "享学";
    private static String lb = "鲁班";
    private static String tl = "图灵";
    private static String dl = "动力节点";// 11 1752
    private static String st = lb;
    // 动力节点 的URL和条件判断特殊处理
    private static boolean isDL = st.equals(dl);

    private Site site = Site.me().setDomain("index.html");

    public static void main(String args[]) {
        String url;
        if (isDL) {
            url = "http://www.lqkweb.com/list?q=" + st + "%20java%20VIP&p=";
        } else {
            url = "http://www.lqkweb.com/list?q=腾讯%20" + st + "%20java%20VIP&p=";
        }
        // url = "http://www.lqkweb.com/list?q=Netty%20傅健&p=";
        for (int i = start; i <= pageCount; i++) {
            System.out.println(i);
            url += i;
            Spider.create(new BaiduDiskSpider()).addUrl(url)
                .addPipeline((resultItems, task) -> {}).run();
            if (arr.size() > 0) {
                for (String thisUrl : arr) {
                    Spider.create(new PageProcessor() {
                        @Override
                        public void process(Page page) {
                            List<Selectable> nodes = page.getHtml().xpath("//div[@class='modal-footer']/*").nodes();
                            System.out.println(nodes.get(1).xpath("/a/@href").get() + "   " + nodes.get(0).xpath("/span/text()").get());
                        }

                        @Override
                        public Site getSite() {
                            return Site.me().setDomain("index.html");
                        }
                    }).addUrl(thisUrl.replace(" ", "%20"))
                        .addPipeline((resultItems, task) -> {}).run();
                }
                arr.clear();
            }
        }
    }

    @Override
    public void process(Page page) {
        List<String> texts = page.getHtml().xpath("//h4[@class='job-title']/a/allText()").all();
        List<String> hrefs = page.getHtml().xpath("//h4[@class='job-title']/a/@href").all();
        for (int i = 0; i < 20; i++) {
            String text = texts.get(i);
            // if (isDL) {
            //     if (text.contains(st) && text.toLowerCase().contains("vip")) {
            //         String childUrl = "http://www.lqkweb.com" + hrefs.get(i);
            //         String sss = "[" + text + "]--" + childUrl;
            //         arr.add(childUrl);
            //         System.out.println(sss);
            //     }
            // } else {
            //     if (text.contains(st)) {
            //         String childUrl = "http://www.lqkweb.com" + hrefs.get(i);
            //         String sss = "[" + text + "]--" + childUrl;
            //         arr.add(childUrl);
            //         System.out.println(sss);
            //     }
            // }
            if (text.contains("傅健")) {
                String childUrl = "http://www.lqkweb.com" + hrefs.get(i);
                String sss = "[" + text + "]--" + childUrl;
                arr.add(childUrl);
                System.out.println(sss);
            }
        }

    }

    private void insetDate(List<String> linkList, List<String> contentList, List<String> dateList) {

        // this.webMegicService = (WebMegicService) WebMegicTimer.getBean("webMegicService");
        // this.userService= ApplicationContextProvider.getBean(UserService .class);

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String dateFormat = sdf.format(date);
        System.out.println(dateFormat);

        // int isCountInfo = webMegicService.isCountInfo();
        // System.out.println(isCountInfo);
        for (int i = 0; i < linkList.size(); i++) {
            String cd = dateList.get(i).substring(1, dateList.get(i).length() - 1);
            // // 判断是否表中有值
            // if (isCountInfo != 0) {
            // if (cd.equals(dateFormat)) {
            // // 插表
            // webMegicService.insertWebMegicInfo(linkList.get(i),
            // dateList.get(i).substring(1, dateList.get(i).length() - 1), contentList.get(i), "90001");
            // }
            // } else {
            // // 循环插表
            // webMegicService.insertWebMegicInfo(linkList.get(i),
            // dateList.get(i).substring(1, dateList.get(i).length() - 1), contentList.get(i), "90001");
            // }

            printLn(linkList.get(i), dateList.get(i).substring(1, dateList.get(i).length() - 1), contentList.get(i),
                    "90001");
        }
    }

    public void printLn(Object... args) {
        Arrays.stream(args).forEach(System.out::println);
    }

    @Override
    public Site getSite() {
        // site.setCharset("UTF-8");
        return site;
    }

    @Scheduled(cron = "0 0 1 * * ? ")
    public void scheduled() {
        Spider.create(new BaiduDiskSpider()).addUrl("http://dangjian.people.com.cn/GB/394443/index.html")
                .addPipeline(new ConsolePipeline()).run();
    }

    /*
     * @Scheduled(cron="0/5 * * * * ? ") public void scheduled(){ Spider.create(new
     * WebMegicTimer()).addUrl("http://dangjian.people.com.cn/GB/394443/index.html") .addPipeline(new
     * ConsolePipeline()).run(); }
     */

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        BaiduDiskSpider.applicationContext = applicationContext;

    }

    /**
     * 获取applicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通过name获取 Bean.
     *
     * @param name
     * @return
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    /**
     * 通过class获取Bean.
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

}
